# -*- coding: utf-8 -*-
from playwright.sync_api import sync_playwright, expect, Page
from interface.page_login_save_cookie import page_login_save_cookies,跳转

URL_P = "https://pingcode.com/signin"
URL_CS = "http://172.24.131.201:8081/asset/#/login"

# # 使用方法1
# def test_pw_test():
#     # 实例化playwright
#     playwright = sync_playwright().start()
#     # 调用浏览器,headless=False 非headless模式（即有头模式）启动Chromium浏览器,如果headless=Ture则为无界面
#     # slow_mo 减慢操作速度
#     browser = playwright.chromium.launch(headless=False, slow_mo=100)
#     # 上下文 设置浏览器窗口大小 record_video_size={"width": 1920,"height": 1080,}
#     context = browser.new_context()
#     # 不会共享cookies、本地存储等数据
#     # page = browser.new_page()
#     # 页面共享cookies、缓存和其他本地存储的数据
#     # context.wait_for_event('page') # 等待事件触发并将其值传递给谓词函数
#     page = context.new_page()
#
#     # page.set_extra_http_headers(headers=headers) # 设置的特定于页面的额外 HTTP 标头合并
#     # 访问浏览器
#     page.goto(URL_P)
#     # # 暂停，弹窗选择代码框
#     # page.pause()
#
#     page.get_by_role("textbox", name="请输入手机号/邮箱").type("m18515393438@163.com",delay=200)
#     page.get_by_role("textbox", name="请输入登录密码").fill("chenshi0802!")
#     page.get_by_role("button", name="登录").click()
#
#     # 关闭上下文
#     context.close()
#     # 关闭浏览器
#     browser.close()


# 使用方法2 page: Page ，context

def f_test_pw_test1():
    playwright = sync_playwright().start()
    browser = playwright.chromium.launch(headless=False, slow_mo=100)
    context = browser.new_context()
    page = context.new_page()
    page.goto(URL_P)
    # # 暂停，弹窗选择代码框
    # page.pause()
    page.get_by_role("textbox", name="请输入手机号/邮箱").type("m18515393438@163.com",delay=200)
    page.get_by_role("textbox", name="请输入登录密码").fill("chenshi0802!")
    page.get_by_role("button", name="登录").click()
    expect(page).to_have_title("工作台 - PingCode")


def pw_test(page: Page):
    page.goto("https://pingcode.com/signin")
    # page.get_by_role("link", name="帐号密码登录").click()
    page.get_by_role("textbox", name="请输入手机号/邮箱").type("m18515393438@163.com", delay=100)
    page.get_by_role("textbox", name="请输入登录密码").fill("chenshi0802!")
    page.get_by_role("button", name="登录").click()
    page.wait_for_timeout(2000)
    # 使用expect断言
    try:
        expect(page).to_have_title("工作台 - PingCode", timeout=10000)  # 增加超时时间到10秒
    except AssertionError as e:
        print(f"Assertion failed: {e}")
        # 截图保存当前状态用于调试
        page.screenshot(path="failed_test_screenshot.png")
        raise
    # print(page.title())
    # first 获取第一个
    page.screenshot(path="debug.png")
    expect(page.get_by_text("工作台").first).to_be_attached(timeout=3000)
    # 使用assert进行断言，返回ture和false
    assert page.get_by_text("工作台").count()


def test_pw1_test(page):
    # page_login(page, "m18515393438@163.com", "chenshi0802!")
    page_login_save_cookies(page, "m18515393438@163.com", "chenshi0802!")
    page.wait_for_timeout(5000)

    跳转(page, "/ship/products")
    page.get_by_text("新建产品").click()

    page.get_by_placeholder("")
    page.wait_for_timeout(5000)



